<template>
  <div class="photoinfo">
    <!-- 图片描述区域 -->
    <h3>{{ photoinfo.title }}</h3>
    <p class="subtitle">
      <span>发表时间：{{ photoinfo.add_time | datefmt }}</span>
      <span>点击：{{ photoinfo.click }}次</span>
    </p>
    <hr>
    <!-- 缩略图预览区域 -->
    <div class="thumbs">
      <img
        v-for="(item,index) in list"
        :key="item.id"
        :src="item.src"
        alt
        @click="handleImage(index)"
      >
    </div>
    <!-- 图片内容区域 -->
    <div class="content" v-html="photoinfo.content"></div>
    <comment :to="id"/>
  </div>
</template>
<script>
import comment from "../../components/subcomponents/comment";
import { Toast } from "vant";
import { ImagePreview } from "vant";
export default {
  data() {
    return {
      id: this.$route.params.id,
      photoinfo: {},
      list: []
    };
  },
  created() {
    this.getimageInfo();
    this.getthumimages();
  },
  components: {
    comment
  },
  methods: {
    /* 
      获取详情数据
    */
    async getimageInfo() {
      const {
        data: { status, message }
      } = await this.$http.get("api/getimageInfo/" + this.id);
      if (status == 0) {
        this.photoinfo = message;
      } else {
        Toast("获取失败");
      }
    },
    /*    
    获取缩略图
    */
    async getthumimages() {
      const {
        data: { status, message }
      } = await this.$http.get("api/getthumimages/" + this.id);
      if (status == 0) {
        this.list = message;
      } else {
        Toast("获取失败");
      }
    },
    /*
    图片预览  
    */
    handleImage(index) {
      const images = [];
      //打list里的数据遍历到iamges
      this.list.forEach(element => {
        if (element.src) {
          images.push(element.src);
        }
      });
      ImagePreview({
        // 预览的图片 它是一个数组
        // images:images
        images,
        // 当前预览的图片的索引
        startPosition: index
      });
    }
  }
};
</script>
<style lang="less">
.photoinfo {
  padding: 3px;
  h3 {
    color: #26a2ff;
    font-size: 15px;
    text-align: center;
    margin: 15px 0;
  }
  .subtitle {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
  }

  .content {
    font-size: 13px;
    line-height: 30px;
  }

  .thumbs {
    display: flex;
    justify-content: space-around;
    img {
      width: 100px;
      height: 100px;
    }
  }
}
</style>
